//
//  ViewController.m
//  UI10_UITableView
//
//  Created by wyx on 16/3/22.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "TableViewOfContact.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

//联系人数组
@property (nonatomic, retain) NSArray *arrOfConrent;


@end

@implementation ViewController
- (void)dealloc{
    [_arrOfConrent release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor blueColor];
    self.navigationItem.title = @"title";
    [self createTableView];
    [self handleDate];
}
#pragma mark - 知识点1:UITableView
- (void)createTableView{
    TableViewOfContact *tableView = [[TableViewOfContact alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    [tableView release];
    //指定代理人
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorColor = [UIColor redColor];
}
#pragma mark - 知识点2:TableViewDateSpurce 相关协议方法
/** 必须实现协议方法:1/2 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrOfConrent.count;
}
/** 必须实现协议方法;2/2 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //显示最下面 item 行数和将要显示的 item 行数
    //NSLog(@"%ld,%ld",indexPath.section, indexPath.row);
    
    //static 在静态区存储变量,且只初始化一次,内存进行了优化
    static NSString *pool1 = @"pool1";
    //从重用池中取 cell 对象
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pool1];
    //判断 cell 是否为 nil
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:pool1];
    }
    //cell 赋值
    cell.textLabel.text = [[self.arrOfConrent objectAtIndex:indexPath.row]objectForKey:@"name"];
    cell.imageView.image = [UIImage imageNamed:[[self.arrOfConrent objectAtIndex:indexPath.row]objectForKey:@"image"]];
    cell.detailTextLabel.text = [[self.arrOfConrent objectAtIndex:indexPath.row]objectForKey:@"phone"];
    return cell;
}
#pragma mark - Date
- (void)handleDate{
    self.arrOfConrent = @[@{@"name":@"zhangsan",@"image":@"1.jpg",@"phone":@"12345"},
          @{@"name":@"lisi",@"image":@"2.jpg",@"phone":@"12345"},
          @{@"name":@"Tom",@"image":@"3.jpg",@"phone":@"12345"},
          @{@"name":@"wangwu",@"image":@"4.jpg",@"phone":@"12345"}];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
